Google アクセス トークンを使用してユーザー プロファイルを取得するにはどうすればよいですか (How do I get user profile using Google Access Token)


問題の説明

Google アクセス トークンを使用してユーザー プロファイルを取得するにはどうすればよいですか (How do I get user profile using Google Access Token)

i'm testing getting user information by google access token

http://www.mawk3y.net/glogin

after clicking sign in button i get redirected to 

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=access_token_here

And get some JSON data like this

{
"issued_to": "my client id.apps.googleusercontent.com",
"audience": "my client id.apps.googleusercontent.com",
"user_id": "user id here",
"scope": "https://www.googleapis.com/auth/plus.login",
"expires_in": 3596,
"access_type": "online"
}

now i need to know how to extract user name , address and email any help please ?

thanks in advance


リファレンスソリューション

方法 1:

Try this one:

 var url = 'https://www.googleapis.com/plus/v1/people/me?access_token={access_token}';

  $.ajax({
    type: 'GET',
    url: url,
    async: false,
    success: function(userInfo) {
      //info about user
      console.log(userInfo);
      console.log('test');
    },
    error: function(e) {
      console.log('error');

    }
  });

方法 2:

You can verify the auth token received after google signin on your server using this api

Request

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={accces_token}

Response

{

  "email_verified": "true",
  "email": "abhinav.xxx@gmail.com",
  "name": "abhinav srivastava",
  "picture": "https://lh3.googleusercontent.com/‑xgD_zFj1EgY/AAAAAAAAAAI/AAAAAAAACZ0/fnecSQ03o0Y/s96‑c/photo.jpg",
  "given_name": "abhinav",
  "family_name": "srivastava",
  "locale": "en",
  ...
  ...
}

source

方法 3:

I had the same issue. I wanted to extract the user information. But couldn't get the exact link to hit. Then I went through the code for Passport Google Strategy at Line number 54.

My scopes were ['profile', 'email']

GET Request

https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}

Response

{
  "sub": "23423....",
  "name": "John Doe",
  "given_name": "John",
  "family_name": "Doe",
  "picture": "<Profile picture URL>",
  "email": "john.doe@gmail.com",
  "email_verified": true,
  "locale": "en"
}

方法 4:

You need to use the access token (you get it in the redirect url) to access Google's People API. Check out the specs here.

You might find Google's OAuth2 playground pretty useful to get an idea of how to use access tokens to access Google's APIs. 

Good luck!

方法 5:

If you want token_info, pass tokeninfo as param

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=ya29.a0AfH6SMArZZITzn‑...

If you want all user info, pass userinfo as param

https://www.googleapis.com/oauth2/v3/userinfo?access_token=ya29.a0AfH6SMArZZITzn‑...

tokeninfo reponse

{
    "azp": "",
    "aud": "",
    "sub": "",
    "scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
    "exp": "",
    "expires_in": "",
    "email": "",
    "email_verified": "",
    "access_type": ""
}

userinfo reponse

{
    "sub": "",
    "name": "",
    "given_name": "",
    "family_name": "",
    "picture": "",
    "email": "",
    "email_verified": ,
    "locale": ""
}

Don't fogot to pass https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile as scope

var params = {
  'client_id': '',
  'redirect_uri': '',
  'response_type': 'token',
  'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
};

(by jq beginnerbetmakhHimalayanCoderAsutosh PandaMiguel AndresArunjith R S)

リファレンスドキュメント

  1. How do I get user profile using Google Access Token (CC BY‑SA 3.0/4.0)

#google-oauth #signing #google-api






関連する質問

OAuth 2.0を使用したGoogleログイン:不明なホスト例外の取得 (OAuth 2.0 Using for Google Login: Getting Unknown Host Exception)

Google OAuth を Java サーブレットと統合するにはどうすればよいですか? (How do I integrate google OAuth with java servlets?)

インストール済みアプリケーションに OAuth 2.0 を使用する (Using OAuth 2.0 for Installed Applications)

LAN 内で oauth 自動化を行う Grails アプリケーションをテストできない (Cannot test Grails application which has oauth autontication inside LAN)

Google OAuth トークンのリンクは何ですか? (What is the link for Google OAuth tokens?)

ページ無限時間リロードの問題 (Page infinite time reload issue)

個別のプライベート アプリケーションの OAuth2 同意設定 (OAuth2 consent setup for individual private application)

Chrome 拡張機能 OAuth2 SingIn の問題 - このアカウントのサービスが無効になっています (Chrome extension OAuth2 SingIn issue - service has beend disabled for this account)

TypeScript: Google "One Tap" 名前 'googleyolo' が見つかりません (TypeScript: Google "One Tap" Cannot find name 'googleyolo')

NodeJS、Google APIを使用してリフレッシュトークンで新しいトークンを取得するには? (NodeJS, How to get new token with refresh token using google api?)

Google OAuth2 REST API で「revoke」リクエストの成功/失敗のコールバックを取得するには? (How to get success/fail callback for `revoke` request with Google OAuth2 REST API?)

Google Oauth2.0 エンドポイントが 404 エラーを返す (Google Oauth2.0 endpoint returns 404 Error)







コメント